﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Data.SqlClient;

public partial class ajax_addproject : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] != null && Session["permission"] != null
            && ((int)Session["permission"]) == 2 || ((int)Session["permission"]) == 3)
        {

            string name_project = Request["name"];
            if (comm.StandardString(name_project).Length == 0)
            {
                Response.Write("FAIL");
                return;
            }
            string des_project = comm.to_sql(Request["des"], "string");
            string start = Request["start"];


            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "dd/MM/yyyy";
            dtfi.DateSeparator = "/";
            if (start.Length > 0)
            {
                try
                {
                    DateTime dateStart = Convert.ToDateTime(start, dtfi);
                    start = comm.to_sql(dateStart.Day.ToString() + "/" + dateStart.Month + "/" + dateStart.Year, "date");
                }
                catch
                {
                    start = null;
                }
            }
            else
                start = null;

            string end = Request["end"];
            if (end.Length > 0)
            {
                try
                {
                    DateTime dateEnd = Convert.ToDateTime(end, dtfi);
                    end = comm.to_sql(dateEnd.Day.ToString() + "/" + dateEnd.Month + "/" + dateEnd.Year, "date");
                }
                catch
                {
                    end = null;
                }
            }
            else
                end = null;

            //string priority = comm.to_sql(Request["priority"], "number");
            string priority = Request["priority"];
            try
            {
                int pri = Int32.Parse(priority);
                if (pri <= 0)
                    throw new Exception();
                priority = comm.to_sql(priority, "number");
            }
            catch
            {
                priority = comm.to_sql("1", "number");
            }
            string isPublic = (Request["public"] == "true") ? "1" : "0";
            string id_user = comm.to_sql(Session["id_user"].ToString(), "number");



            // check project is exist
            SqlParameter[] someparams = new SqlParameter[3];
            SqlParameter param = new SqlParameter();
            param.Value = Int32.Parse(Session["id_user"].ToString());
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@userId";
            param.Direction = ParameterDirection.Input;
            someparams[0] = param;

            param = new SqlParameter();
            param.Value = name_project;
            param.SqlDbType = SqlDbType.NVarChar;
            param.Size = 255;
            param.ParameterName = "@projectName";
            param.Direction = ParameterDirection.Input;
            someparams[1] = param;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@projectId";
            param.Value = -1;
            param.Direction = ParameterDirection.Output;
            someparams[2] = param;


            dal.ExecuteSP("proCheckExistProject_GET", someparams);
            if (someparams[2].Value.ToString() == "")
            {
                string sql = "";
                name_project = comm.to_sql(name_project, "string");
                if (start != null && end != null)
                    sql = "INSERT INTO project(name,start,[end],createdby,accessable,priority,[public],description,completepercent) " +
                          " VALUES(" + name_project + "," + start + "," + end + "," + id_user + ",1," + priority + "," + isPublic + "," + des_project + ",0" + ")";
                else if (start != null)
                    sql = "INSERT INTO project(name,start,createdby,accessable,priority,[public],description,completepercent) " +
                          " VALUES(" + name_project + "," + start + "," + id_user + ",1," + priority + "," + isPublic + "," + des_project + ",0" + ")";
                else
                    sql = "INSERT INTO project(name,createdby,accessable,priority,[public],description,completepercent) " +
                          " VALUES(" + name_project + "," + id_user + ",1," + "," + priority + "," + isPublic + "," + des_project + ",0" + ")";

                if (dal.Execute(sql) == 1)
                    Response.Write("OK");
                else
                    Response.Write("FAIL");
                return;
            }
            else if ((int)(someparams[2].Value)!=0)
            {
                Response.Write("DUPLICATE");
            }
            else
                Response.Write("FAIL");
        }
    }
}
